<template>
  <div class="login-container">
    <h2>登录</h2>
    <form @submit.prevent="handleLogin">
      <div class="form-group">
        <label for="username">用户名:</label>
        <input
          id="username"
          v-model="username"
          type="text"
          placeholder="请输入用户名"
          required
        />
      </div>
      <div class="form-group">
        <label for="password">密码:</label>
        <input
          id="password"
          v-model="password"
          type="password"
          placeholder="请输入密码"
          required
        />
      </div>
      <div v-if="errorMessage" class="error-message">
        {{ errorMessage }}
      </div>
      <button type="submit" :disabled="isSubmitting">
        {{ isSubmitting ? '登录中...' : '登录' }}
      </button>
    </form>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'

const router = useRouter()
const username = ref('')
const password = ref('')
const isSubmitting = ref(false)
const errorMessage = ref('')

const handleLogin = async () => {
  isSubmitting.value = true
  errorMessage.value = ''
  if (username.value === 'admin' && password.value === '123456') {
    router.push('/login-success')
  } else {
    errorMessage.value = '用户名或者密码错误'
  }
  isSubmitting.value = false
}
</script>

<style scoped>
.login-container {
  max-width: 500px;
  margin: 0 auto;
  padding: 30px;
  border: 1.5px solid #ccc;
  border-radius: 10px;
  box-shadow: 0 0 15px rgba(0, 0, 0, 0.1);
}

h2 {
  font-size: 24px;
}

.form-group {
  margin-bottom: 20px;
  display: flex;
  align-items: center;
}

label {
  margin-bottom: 0;
  font-weight: bold;
  margin-right: 15px;
  min-width: 80px;
  font-size: 16px;
}

input {
  flex: 1;
  padding: 12px;
  border: 1.5px solid #ccc;
  border-radius: 6px;
  box-sizing: border-box;
  font-size: 16px;
}

button {
  background-color: #42b983;
  color: white;
  padding: 15px 20px;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  font-size: 16px;
}

button:disabled {
  background-color: #cccccc;
  cursor: not-allowed;
}

.error-message {
  color: red;
  margin: 10px 0;
  font-size: 14px;
}
</style>